/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.mdc.hearts.war.authorization;

import it.mdc.hearts.war.room.SessionUserBean;
import it.mdc.hearts.war.room.core.UserInfo;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import it.mdc.hearts.ejb.database.Users;
import it.mdc.hearts.ejb.database.UsersFacadeLocal;
import org.primefaces.context.RequestContext;

/**
 *
 * @author Administrator
 */
@ManagedBean(name = "createUserBean")
@RequestScoped
public class CreateUserBean implements Serializable  
{
    @EJB
    private UsersFacadeLocal usersFacade;
    
    @ManagedProperty(value = "#{sessionUserBean}") // MY SESSION BEAN
    private SessionUserBean userSession;
    
    private String username;
    private String password;
    private String passwordVerify;

    public void setUserSession(SessionUserBean userSession) {
        this.userSession = userSession;
    }
    
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPasswordVerify() {
        return passwordVerify;
    }

    public void setPasswordVerify(String passwordVerify) {
        this.passwordVerify = passwordVerify;
    }
    
     public String createUser() 
     {
        boolean loggedIn = false;
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("ID", null);
        
        Users user = usersFacade.getUserByUsername(username);
        if (user == null) 
        {
            if (password.equals(passwordVerify)) 
            {
                user = new Users();
                user.setUsername(username);
                user.setPasswd(password);
                user.setPoint(0);

                try 
                {
                    usersFacade.create(user);

                    FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("ID", user.getId());
                    
                    userSession.setID(user.getId()); 
                    
                    UserInfo userInfo = new UserInfo();
                    userInfo.setUserID(user.getId());
                    userInfo.setUsername(user.getUsername());
                    userInfo.setScore(user.getPoint());
                    
                    userSession.InitUser(userInfo);
                    
                    loggedIn = true;
                    
                    RequestContext context = RequestContext.getCurrentInstance(); 
                    context.addCallbackParam("loggedIn", loggedIn);
                    return "loginSuccess";   
                }
                catch (Exception e) 
                {
                    FacesContext.getCurrentInstance().addMessage("createForm", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Register Failed!", "Unexpected error when creating your account.  Please contact the system Administrator"));
                }
            }
            else{
                FacesContext.getCurrentInstance().addMessage("createForm", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Register Failed!", "The specified passwords do not match"));
            }  
        } 
        else 
        {
            FacesContext.getCurrentInstance().addMessage("createForm", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Username '" + username + "' already exists!  ", "Please choose a different username"));
        }
        
        RequestContext context = RequestContext.getCurrentInstance(); 
        context.addCallbackParam("loggedIn", loggedIn);
        return null;
    }
    
}
